package fileAndIO.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionSample {
    public static void main(String[] args) {
        InputStream is = null;
        OutputStream os = null;
        try {
            URL url = new URL("http://i0.hdslb.com/bfs/banner/096a467941c5e1d9082cbd24c072972013d8dc1e.png");
            URLConnection connection = url.openConnection();
            is = connection.getInputStream();
            os = new FileOutputStream("D:/aa.jpg");
            int len = 0;
            byte[] bs = new byte[1024];
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
